package v1.com.autoresource.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import v1.com.autoresource.aop.SelectDataSourceAspect;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: learning
 * @Package: com.autoresource.config
 * @ClassName: DataResourceConfiguration
 * @Description:
 * @Date: 2021/11/1 8:01 下午
 * @Version: 1.0
 */
@Configuration
@MapperScan(basePackages = {"v1.com.autoresource.mapper"})
public class DataResourceConfiguration {

    @Bean
    public DataSource masterDataSource() {
        HikariConfig conf = new HikariConfig();
        conf.setUsername("root");
        conf.setPassword("root");
        conf.setJdbcUrl("jdbc:mysql://localhost:3301/trade?allowMultiQueries=true&rewriteBatchedStatements=true");
        conf.addDataSourceProperty("rewriteBatchedStatements", true);
        return new HikariDataSource(conf);
        // return DataSourceBuilder.create().build();
    }


    @Bean
    public DataSource slaveDataSource() {
        HikariConfig conf = new HikariConfig();
        conf.setUsername("root");
        conf.setPassword("root");
        conf.setJdbcUrl("jdbc:mysql://localhost:3302/trade");
        return new HikariDataSource(conf);
    }

    @Bean
    public DataSource dynamicDataSource() {
        DynamicDataSource dataSource = new DynamicDataSource();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DynamicDataResourceHolder.MASTER_DATA_SOURCE, masterDataSource());
        targetDataSources.put(DynamicDataResourceHolder.SLAVE_DATA_SOURCE, slaveDataSource());
        dataSource.setTargetDataSources(targetDataSources);
        dataSource.setDefaultTargetDataSource(masterDataSource());
        return dataSource;
    }


    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dynamicDataSource());
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:mysql/mapper/*.xml"));
        return sqlSessionFactoryBean;
    }

    @Bean
    public PlatformTransactionManager pbbTransactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }


    @Bean
    public SelectDataSourceAspect selectDataSourceBeanPostProcessor() {
        return new SelectDataSourceAspect();
    }


    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
